package com.hsmw.common.context;

import com.htn.common.core.constant.CommonConstants;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @author hongfei
 */
public class BaseContextHandler {
	private static ThreadLocal<Map<String, Object>> threadLocal = new ThreadLocal<>();

	private static void set(String key, Object value) {
		Map<String, Object> map = threadLocal.get();
		if (map == null) {
			map = new HashMap<>();
			threadLocal.set(map);
		}
		map.put(key, value);
	}

	public static void setOpenId(String openId) {
		set(CommonConstants.CONTEXT_KEY_OPEN_ID, openId);
	}

	public static void setIP(String ip) {
		set(CommonConstants.CONTEXT_KEY_IP, ip);
	}

	public static void setUserId(String userID) {
		set(CommonConstants.CONTEXT_KEY_USER_ID, userID);
	}

	public static void setClientId(String clientId) {
		set(CommonConstants.CONTEXT_KEY_CLIENT_ID, clientId);
	}

	;

	public static void setResponse(HttpServletResponse response) {
		set(CommonConstants.CONTEXT_KEY_RESPONSE, response);
	}

	public static void setUserRole(String userRole) {
		CommonConstants.UserRole role;
		try {
			role = CommonConstants.UserRole.valueOf(userRole);
		} catch (Exception e) {
			role = CommonConstants.UserRole.UNKNOWN;
		}
		set(CommonConstants.CONTEXT_KEY_USER_ROLE, role);
	}

	public static void setUserRole(CommonConstants.UserRole userRole) {
		set(CommonConstants.CONTEXT_KEY_USER_ROLE, userRole);
	}

	public static void setRequest(HttpServletRequest request) {
		set(CommonConstants.CONTEXT_KEY_REQUEST, request);
	}

//	public static void setValidator(List<IAuthValidator> validatorList) {
//		set(CommonConstants.CONTEXT_KEY_VALIDATOR, validatorList);
//	}

	public static void setExtraToken(String token) {
//        if(StrUtil.isBlank(token) || get(CommonConstants.CONTEXT_KEY_EXTRA_TOKEN) != null){
//            return;
//        }

		set(CommonConstants.CONTEXT_KEY_EXTRA_TOKEN, token);
		HttpServletResponse response = (HttpServletResponse) get(CommonConstants.CONTEXT_KEY_RESPONSE);
		if (response != null) {
			//TODO 这里要改
//            Cookie tokenCookie = new Cookie(JwtUtil.HEADER_STRING, BaseContextHandler.getExtraToken());
//            tokenCookie.setHttpOnly(true);
//            tokenCookie.setPath("/");
//            response.addCookie(tokenCookie);
		}

	}

	private static Object get(String key) {
		Map<String, Object> map = threadLocal.get();
		if (map == null) {
			map = new HashMap<>();
			threadLocal.set(map);
		}
		return map.get(key);
	}

	public static String getOpenId() {
		return (String) get(CommonConstants.CONTEXT_KEY_OPEN_ID);
	}

	public static String getIp() {
		return (String) get(CommonConstants.CONTEXT_KEY_IP);
	}

	public static String getUserId() {
		return (String) get(CommonConstants.CONTEXT_KEY_USER_ID);
	}

	public static String getClientId() {
		return (String) get(CommonConstants.CONTEXT_KEY_CLIENT_ID);
	}

	public static CommonConstants.UserRole getUserRole() {
		return (CommonConstants.UserRole) get(CommonConstants.CONTEXT_KEY_USER_ROLE);
	}

	public static String getExtraToken() {
		return (String) get(CommonConstants.CONTEXT_KEY_EXTRA_TOKEN);
	}

	public static Map<String, Object> getAll() {
		Map<String, Object> map = threadLocal.get();
		return map == null ? new HashMap<>() : map;
	}

	public static void remove() {
		threadLocal.remove();
	}

}
